package com.tbit.uqbike.client.controller;

import com.tbit.uqbike.client.constant.ResponseCode;
import com.tbit.uqbike.client.exception.BaseException;
import com.tbit.uqbike.client.pojo.AccountConfig;
import com.tbit.uqbike.client.pojo.AppointMent;
import com.tbit.uqbike.client.pojo.AppointMentLog;
import com.tbit.uqbike.client.pojo.User;
import com.tbit.uqbike.client.service.AccountConfigService;
import com.tbit.uqbike.client.service.AppointMentService;
import com.tbit.uqbike.client.service.TokenService;
import com.tbit.uqbike.client.util.Assert;
import com.tbit.uqbike.client.util.OutputUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 预约
 *
 * @Author: chen
 * @Description: 预约
 * @Date: 创建于 14:25 2020/8/17
 */
@RestController
@RequestMapping("/appointMent")
public class AppointMentController {
    @Autowired
    private AppointMentService appointMentService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private AccountConfigService accountConfigService;


    /**
     * 分页展示预约记录
     *
     * @param token    令牌
     * @param pageNO   页码
     * @param rowCount 当前第几页
     * @param response 响应
     * @ignoreParams request
     */
    @RequestMapping("/queryPage")
    public void queryPage(String token, @RequestParam(defaultValue = "1") Integer pageNO, @RequestParam(defaultValue = "10") Integer rowCount,
                          HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            User user = tokenService.getUser(token);
            List<AppointMentLog> list = appointMentService.queryPage(user.getUserId(), pageNO, rowCount);
            Integer total = appointMentService.queryCount(user.getUserId());
            map.put("total", total);
            map.put(ResponseCode.RET, ResponseCode.SUCC);
            map.put(ResponseCode.DATA, list);
        } catch (BaseException e) {
            ResponseCode.printException(map, e, request);
        }
        OutputUtil.renderText(request, response, map);
    }

    /**
     * 预约
     *
     * @param appointMent 预约实体类
     * @param token       令牌
     * @param response    响应
     * @ignoreParams request
     */
    @RequestMapping("/add")
    public void add(AppointMent appointMent, String token, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            User user = tokenService.getUser(token);
            //Assert.notNull(appointMent.getAccountId(),"-10001","平台Id不能为空");
            Assert.notNull(appointMent.getUserCode(), "-30006", "用户码不能为空");
            appointMent.setUserId(user.getUserId());
            appointMentService.add(appointMent);
            map.put(ResponseCode.RET, ResponseCode.SUCC);
        } catch (BaseException e) {
            ResponseCode.printException(map, e, request);
        }
        OutputUtil.renderText(request, response, map);
    }

    /**
     * 取消预约
     *
     * @param token    令牌
     * @param response 响应
     * @ignoreParams request
     */
    @RequestMapping("/del")
    public void del(String token, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            User user = tokenService.getUser(token);
            appointMentService.del(user.getUserId());
            map.put(ResponseCode.RET, ResponseCode.SUCC);
        } catch (BaseException e) {
            ResponseCode.printException(map, e, request);
        }
        OutputUtil.renderText(request, response, map);
    }

    /**
     * 通过用户id获取是否预约
     *
     * @param token    令牌
     * @param response 响应
     * @ignoreParams request
     */
    @RequestMapping("/getByUserId")
    public void getByUserId(String token, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            User user = tokenService.getUser(token);
            AppointMent appointMent = appointMentService.getByUserId(user.getUserId());
            if (appointMent != null) {
                AccountConfig accountConfig = accountConfigService.getByAccountId(appointMent.getAccountId());
                if (accountConfig != null) {
                    appointMent.setTime(accountConfig.getAppointTime());
                }

            }
            map.put(ResponseCode.RET, ResponseCode.SUCC);
            map.put(ResponseCode.DATA, appointMent);
        } catch (BaseException e) {
            ResponseCode.printException(map, e, request);
        }
        OutputUtil.renderText(request, response, map);
    }
}
